export default class TimerManager {
    timers: any
    timerIDIndex: number = 1
    speed: number = 1
    constructor() {
        this.timers = new Map();
    }

    // 创建一个定时器（可以模拟 setTimeout 或 setInterval）
    createTimer(type, callback, delay, remaining, { repeat = false } = {}) {
        const timer = {
            type,
            callback,
            delay,
            remaining: remaining,
            repeat,
            speed: this.speed,
            paused: false,
            lastStart: Date.now(),
            timeoutId: null,
        };

        // 设置定时器并保存 ID
        const start = () => {
            timer.timeoutId = setTimeout(() => {
                if (!timer.paused) {
                    timer.callback();
                    if (timer.repeat) {
                        timer.lastStart = Date.now();
                        if (typeof delay == "number") {
                            timer.remaining = timer.delay;
                        } else if (typeof delay == "function") {
                            timer.remaining = timer.delay();
                        }

                        start();
                    } else {
                        delete this.timers[timer.timeoutId];
                    }
                }
            }, timer.remaining / timer.speed);
        };

        // 开始定时器
        start();

        // 保存定时器到管理器
        const timerId = this.timerIDIndex++
        this.timers[timerId] = timer;
        return timerId;
    }

    // 停止指定定时器
    clear(timerId) {
        const timer = this.timers[timerId];
        if (timer) {
            clearTimeout(timer.timeoutId);
            delete this.timers[timerId];
        }
    }

    // 暂停定时器
    pause(timerId) {
        const timer = this.timers[timerId];
        if (timer && !timer.paused) {
            clearTimeout(timer.timeoutId);
            timer.paused = true;
            timer.remaining -= (Date.now() - timer.lastStart) * timer.speed;
        }
    }

    // 继续定时器
    resume(timerId) {
        const timer = this.timers[timerId];
        if (timer && timer.paused) {
            timer.paused = false;
            timer.lastStart = Date.now();
            timer.timeoutId = setTimeout(() => {
                timer.callback();
                if (timer.repeat) {
                    timer.lastStart = Date.now();
                    timer.remaining = timer.delay;
                    this.resume(timerId); // 继续循环
                } else {
                    delete this.timers[timerId];
                }
            }, timer.remaining / timer.speed);
        }
    }

    // 修改定时器的速度
    setSpeed(timerId, speed) {
        const timer = this.timers[timerId];
        if (timer) {
            if (!timer.paused) this.pause(timerId);
            timer.speed = speed;
            this.resume(timerId);
        }
    }

    setAllSpeed(speed) {
        this.speed = speed
        Object.values(this.timers).forEach((timer: any) => {
            if (timer) {
                if (!timer.paused) this.pause(timer.timeoutId);
                timer.speed = speed;
                this.resume(timer.timeoutId);
            }
        })

    }

    // 创建一个一次性定时器（类似于 setTimeout）
    setTimeout(callback, delay) {
        if (typeof delay == "number") {
            return this.createTimer("timeout", callback, delay, delay, { repeat: false });
        } else if (typeof delay == "function") {
            let remaining = delay()
            return this.createTimer("timeout", callback, delay, remaining, { repeat: false });
        }
    }

    // 创建一个循环定时器（类似于 setInterval）
    setInterval(callback, delay) {
        if (typeof delay == "number") {
            return this.createTimer("interval", callback, delay, delay, { repeat: true });
        } else if (typeof delay == "function") {
            let remaining = delay()
            return this.createTimer("interval", callback, delay, remaining, { repeat: true });
        }
    }

    stop() {
        Object.values(this.timers).forEach((timer: any) => {
            if (timer) {
                this.clear(timer.timeoutId);
                delete this.timers[timer.timeoutId];
            }
        })
    }
}




